<?php

namespace app\merchant\controller;

use app\merchant\model\User as UserModel;

/**
 * 用户管理类
 * Class Index
 * @package app\merchant\controller
 */
class User extends Base
{

    /**
     * 用户列表
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->postData();

        $model = new UserModel;
        $data = $model->getList($param);

        return $this->renderSuccess($data,'查询成功');

    }


    /**
     * 删除用户
     * @param $user_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        if(!isset($param['user_id']) || isInt($param['user_id'])){
            return $this->renderError([],'参数错误');
        }
        // 用户详情
        $model = UserModel::detail($param['user_id']);
        if ($model->setDelete()) {
            return $this->renderSuccess([],'删除成功');
        }
        return $this->renderError([],$model->getError() ?: '删除失败');
    }


    /**
     * 用户详情
     * @param $user_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        if(!isset($param['user_id']) || isInt($param['user_id'])){
            return $this->renderError([],'参数错误');
        }
        // 用户详情
        $model = UserModel::merchantDetail($param['user_id']);
        if(!$model){
            return $this->renderError([], '用户不存在，请刷新重试');
        }
        return $this->renderSuccess($model,'查询成功');

    }

    /**
     * 用户充值
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function recharge()
    {
        $param = $this->postData();
        if(!isset($param['user_id']) || isInt($param['user_id'])){
            return $this->renderError([],'参数错误');
        }
        $model = UserModel::merchantDetail($param['user_id']);

        if ($model->recharge($param['money'],$param['remark'])) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');

    }


    /**
     * 分销权限
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function deacler()
    {
        $param = $this->postData();
        if(!isset($param['user_id']) || isInt($param['user_id'])){
            return $this->renderError([],'参数错误');
        }
        $model = UserModel::merchantDetail($param['user_id']);

        if ($model->setDeacler($param['state'])) {

            return $this->renderSuccess([],'操作成功');
        }
        return $this->renderError([],$model->getError() ?: '操作失败');

    }


    /**
     * 代言人
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function staff()
    {
        $param = $this->postData();

        if(!isset($param['user_id']) || isInt($param['user_id'])){
            return $this->renderError([],'参数错误');
        }

        $model = UserModel::merchantDetail($param['user_id']);

        if ($model->setStaff($param['state'])) {

            return $this->renderSuccess([],'操作成功');
        }
        return $this->renderError([],$model->getError() ?: '操作失败');

    }


    /**
     * 微股东
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function shareholder()
    {
        $param = $this->postData();
        if(!isset($param['user_id']) || isInt($param['user_id'])){
            return $this->renderError([],'参数错误');
        }
        $model = UserModel::merchantDetail($param['user_id']);

        if ($model->setShareholder($param['state'])) {

            return $this->renderSuccess([],'操作成功');
        }
        return $this->renderError([],$model->getError() ?: '操作失败');

    }




    


}
